package cn.didi.feidi.androidorder.assembly;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import cn.didi.feidi.androidorder.OrderApplication;
import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.databinding.ViewFeiDiBottomNavigationBinding;

/**
 * Created by jinxiong on 2017/8/19.
 */

public class FeiDiBottomNavigation extends LinearLayout {


    public static final byte HOME_PAGE = 0;
    public static final byte ORDER = 1;
    public static final byte SHOPPING_CAR = 2;
    public static final byte PERSONAL = 3;
    LinearLayout currentLayout;
    ViewFeiDiBottomNavigationBinding binding;


    public FeiDiBottomNavigation(Context context) {
        this(context, null);
    }

    public FeiDiBottomNavigation(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FeiDiBottomNavigation(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public FeiDiBottomNavigation(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {

        if (context == null) {
            context = OrderApplication.getCurActivity();
        }

        if (context == null) {
            return;
        }

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        binding = DataBindingUtil.inflate(inflater, R.layout.view_fei_di_bottom_navigation, this, true);
        currentLayout = binding.bottomNavigationItemHomePageLl;

        for (int i = 0; i < currentLayout.getChildCount(); i++) {
            currentLayout.getChildAt(i).setSelected(true);
        }

    }

    public void setSelectEvent(@IntRange(from = HOME_PAGE, to = PERSONAL) final int itemIndex, final OnClickListener listener) {


        this.getChildAt(itemIndex).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                commentClickEvent(itemIndex);
                listener.onClick(v);
            }
        });
    }

    /**
     * 选择的时候触发
     *
     * @param itemIndex
     */
    private void commentClickEvent(@IntRange(from = HOME_PAGE, to = PERSONAL) int itemIndex) {

        for (int i = 0; i < currentLayout.getChildCount(); i++) {
            currentLayout.getChildAt(i).setSelected(false);
        }

        currentLayout = (LinearLayout) FeiDiBottomNavigation.this.getChildAt(itemIndex);

        int size = currentLayout.getChildCount();
        for (int i = 0; i < size; i++) {
            currentLayout.getChildAt(i).setSelected(true);
        }
    }


}
